
//
//  FLMDeatilListViewController.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/10/8.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMDeatilListViewController.h"

#import "FLMDeatilListModel.h"
#import "FLMDeatilListHeaderView.h"
#import "FLMDeatilsListCell.h"
#import "FLMDeatilsListView.h"
#import "FLMBuyDeailsView.h"
#import "SDCycleScrollView.h"
#import "FLMWebViewController.h"
@interface FLMDeatilListViewController ()<SDCycleScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *indexlunboArr; //轮播

@property (nonatomic, strong) FLMGoodsDetailListModel *goodslistModelArr; //商品详情数组


@property (nonatomic, strong) NSMutableArray *recommendModelArr; //推荐商品数组


@property (nonatomic, strong) FLMBuyDeailsView *buyV;

@end

@implementation FLMDeatilListViewController

//懒加载
- (NSMutableArray *)recommendModelArr
{
    if (!_recommendModelArr) {
        _recommendModelArr = [NSMutableArray array];
    }
    return _recommendModelArr;
}

//懒加载
- (NSMutableArray *)indexlunboArr
{
    if (!_indexlunboArr) {
        _indexlunboArr = [NSMutableArray array];
    }
    return _indexlunboArr;
}


//懒加载
- (FLMGoodsDetailListModel *)goodslistModelArr
{
    if (!_goodslistModelArr) {
        _goodslistModelArr = [[FLMGoodsDetailListModel alloc] init];
    }
    return _goodslistModelArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatCollectionView];
    [self loadShopData];
    
    
    //     self.collectionView.backgroundColor = [UIColor purpleColor];
}

#pragma mark 刷新控件
- (void)setUpRefresh
{
    //header
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadShopData)];
    
    self.collectionView.mj_header.automaticallyChangeAlpha = YES;
    
    [self.collectionView.mj_header beginRefreshing];
}

/**
 *  加载最新数据
 */
- (void)loadShopData
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"uname"] = @"bhzhuo";
    parameters[@"upass"] = [BaseHttpRequest md5HexDigest:[NSString stringWithFormat:@"%@%@",@"123456",@"sos_5941m"]];
    parameters[@"code"] = @"";
    parameters[@"token"] = @"f2c8ec76b30527be791752cab0cf743c";
    parameters[@"h"] = @"141";
    parameters[@"num_iid"] = @(self.num_iid);
    
    __weak typeof(self) weakSelf = self;
    [[XLsn0wNetwork defaultNetwork] POST:DetailURL
                                   token:nil
                                  params:parameters
                                 success:^(NSURLSessionDataTask *task, NSDictionary *JSONDictionary, NSString *JSONString) {
                                     
                                     
                                     // YCFAFNWriteToPlist(JSONDictionary);
                                     
                                     NSLog(@"JSONString===%@", JSONString);
                                     
                                     //返回数据
                                     XLsn0wLog(@"JSONDictionary成功数据===%@", JSONDictionary);
                                     
                                     
                                     FLMDeatilListModel *deatilModels = [FLMDeatilListModel mj_objectWithKeyValues:JSONDictionary];
                                     
                                     weakSelf.goodslistModelArr = deatilModels.msg.goodslist;
                                     
                                     weakSelf.recommendModelArr = [deatilModels.msg.recommend_goods mutableCopy];
                                     
                                     [self creatBottomView];
                                     
                                     
                                     [self.collectionView reloadData];
                                     
                                     [self.collectionView.mj_header endRefreshing];
                                     
                                 }
                                 failure:^(NSURLSessionDataTask *task, NSError *error, NSInteger statusCode, NSString *requestFailedReason) {
                                     //错误原因
                                     XLsn0wLog(@"错误数据requestFailedReason===%@", requestFailedReason);
                                     if (error.code == NSURLErrorCancelled)
                                     {
                                         [XLsn0wHUD showTipText:@"无网络!" superview:self.view duration:1 animated:YES];
                                     }
                                     
                                     [self.collectionView.mj_header endRefreshing];
                                 }];
}

#pragma mark -- 设置collectionView-UI
- (void)creatCollectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumInteritemSpacing = 5;
        layout.minimumLineSpacing = 5;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - 60) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[FLMDeatilsListCell class] forCellWithReuseIdentifier:@"FLMDeatilsListCell"];
        [_collectionView registerClass:[FLMDeatilListHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FLMDeatilListHeaderView"];
        _collectionView.showsHorizontalScrollIndicator = YES;
        _collectionView.showsVerticalScrollIndicator = YES;
        [self.view addSubview:_collectionView];
        
    }
}

#pragma mark - UICollectionViewDelegate
#pragma mark --------------------
#pragma mark 跳转详情页
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    //    FLMGoodslistModel *detailModel = self.goodslistModelArr[indexPath.item];
    //    FLMDeatilViewController *detailVC = [[FLMDeatilViewController alloc] init];
    //
    //    detailVC.num_iid = detailModel.num_iid;
    //
    //    [detailVC setHidesBottomBarWhenPushed:YES];
    //
    //    [self.navigationController pushViewController:detailVC animated:YES];
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    self.collectionView.mj_footer.hidden = self.recommendModelArr.count == 0;
    return self.recommendModelArr.count;
}

#pragma mark --------------------
#pragma mark 设置collectionViewcell数据
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    FLMDeatilsListCell *cell = (FLMDeatilsListCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"FLMDeatilsListCell"forIndexPath:indexPath];
    
    [cell configueDeatilCellWithModel:self.recommendModelArr[indexPath.row]];
    //    cell.backgroundColor = [UIColor blueColor];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat itemWidth = (ScreenW - 15)/2;
    CGFloat itemHeight = itemWidth*4/3;
    return CGSizeMake(itemWidth, itemHeight);
}


#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(ScreenW, ScreenH - 220);
}



- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 0, 5);
}


#pragma mark-底部领券购买视图
-(void)creatBottomView{
    
    FLMBuyDeailsView * buyView = [[FLMBuyDeailsView alloc]initWithFrame:CGRectMake(0, ScreenH-GETSIZE(60), ScreenW, GETSIZE(60))];
   
    
    [buyView configueDeatilWithModel:_goodslistModelArr];
    
    
    [self.view addSubview:buyView];
    
}

/**
 *  设置分区代理数据
 */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        FLMDeatilListHeaderView *headerView =
        [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FLMDeatilListHeaderView"forIndexPath:indexPath];
        
        
        [headerView configueCellWithModel:self.goodslistModelArr];
        
        //设置数据
        //        [headerView configBannarView:self.indexlunboArr menuArr:nil];
        //
        //        //轮播回调事件处理
        //        [headerView setBannarClickBlock:^(NSString *bannarUrl) {
        ////
        //            if (![bannarUrl containsString:@"http"]) return;
        //
        //            FLMWebViewController *webVc = [[FLMWebViewController alloc] init];
        //            webVc.url = [NSURL URLWithString:bannarUrl];
        //            // NSLog(@"进入地址是%@", webVc.url);
        //            [self.navigationController pushViewController:webVc animated:YES];
        //        
        //        }];
        
        return headerView;
    }
    return nil;
}

@end

